<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<search-bar @click="goSearch"></search-bar>
		<!-- 内容区域 -->
		<view class="content-box">
			<!-- 左 -->
			<scroll-view class="left" :style="{height: wh + 'px'}" scroll-y="true" >
				<view :class="[current === index ? 'active' : '']" @click="changeCate(item,index)" v-for="item,index in cateList" :key="index">{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右 -->
			<scroll-view :scroll-top="scrollTop" class="right" :style="{height: wh + 'px'}" scroll-y="true" >
				<view  v-for="item,index in cateList2" :key="index">
					<!-- 标题 -->
					<view class="title">/ {{item.cat_name}} /</view>
					<!-- 内容 -->
					<view class="content">
						<view @click="goList(e)" class="content-item" v-for="e,i in item.children" :key="i">
							<image :src="e.cat_icon" mode="widthFix"></image>
							<text>{{e.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				wh: 0, // 内容区域高度
				current: 0, // 当前高亮的index
				scrollTop: 0, // 滚动条距离顶部的距离
				cateList: [], // 分类列表
				cateList2: [],
			}
		},
		onLoad() {
			this.getCateList()
			// 获取设备的屏幕高度
			const res = uni.getSystemInfoSync()
			// console.log(res)
			this.wh = res.windowHeight - 50
		},
		methods: {
			// 跳转到搜索页面
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			// 跳转到商品列表
			goList(item) {
				uni.navigateTo({
					url: '/subpkg/goods-list/goods-list?cat_id='+item.cat_id
				})
			},
			// 切换分类
			changeCate(item, index) {
				// 切换高亮样式 - active
				this.current = index
				// 切换右侧内容
				this.cateList2 = item.children
				// 恢复右侧滚动位置
				this.scrollTop = this.scrollTop === 0 ? .1 : 0
			},
			// 获取商品分类列表
			async getCateList() {
				const {data: {message, meta}} = await uni.$http.get('/api/public/v1/categories')
				if(meta.status !== 200) return uni.$showMsg()
				this.cateList = message
				this.cateList2 = message[0].children
			}
		}
	}
</script>

<style lang="scss">
.content-box {
	display: flex;
	.left {
		width: 90px;
		view {
			font-size: 12px;
			line-height: 60px;
			text-align: center;
			background-color: #f7f7f7;
		}
		.active {
			background-color: #fff;
			position: relative;
			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				height: 30px;
				width: 2px;
				margin-top: -15px;
				background-color: #ea4350;
				
			}
		}
	}
	.right {
		.title {
			margin: 6px 0;
			font-size: 12px;
			text-align: center;
			font-weight: 700;
		}
		.content {
			display: flex;
			flex-wrap: wrap;
			.content-item {
				width: calc(100% / 3);
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 12px;
				margin-bottom: 6px;
				image {
					width: 60px;
					height: 60px;
				}
			}
		}
	}
}
</style>